package cn.edu.ctbu.firstdata.restcontroller;

import cn.edu.ctbu.firstdata.domain.Student;
import cn.edu.ctbu.firstdata.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/webapi/student")
public class StudentRestController {

    @Autowired
    StudentService studentService;

    /**
     * 获取所有学生的信息，为ajaxlist.html设计
     * @return
     */
    @GetMapping("list")
    public List<Student> GetAll(){
        return  studentService.getAll();
    }

    /**
     * 根据姓名查询学生信息，为ajaxlist.html设计
     * @param name
     * @return
     */
    @GetMapping(value = "getbyname")
    public List<Student> findByName(String name) {
        return studentService.findByName(name);
    }

    /**
     * 插入学生信息 对应/web/api/insert
     * @param student
     * @return
     */
    @PostMapping("/insert")
    public Student insert(Student student) {
        return studentService.insert(student);
    }

    /**
     * 更新学生信息 对应/web/api/update
     * @param student
     * @return
     */
    @PostMapping("/update")
    public Student update(Student student) {
        return studentService.update(student);
    }

    /**
     * 删除学生信息 对应/web/api/delete
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public void delete(@PathVariable Integer id) {
        Student student = studentService.findById(id);
        studentService.delete(student);
    }

    /**
     * 读取url：/webapi/student/get/id
     * @param id
     * @return
     */
    @GetMapping(value = "/get/{id}")
    public Student get(@PathVariable Integer id){
        Student student = studentService.findById(id);
        student.setPassword("");
        return student;
    }
}
